﻿
@{
    
    Layout = null;
}
@Html.Partial("_jsAndCss")
<h2>
   顾客相关</h2>
<div class="demo-info" style="margin-bottom: 10px">
    <div class="demo-tip icon-tip">
        &nbsp;</div>
    <div>
       顾客数据的增删改查</div>
</div>
<table id="dg" title="顾客相关" class="easyui-datagrid" style="width: 1000px; height: 650px"
    url="@Url.Action("GetAllCustomers", "customer")" toolbar="#toolbar" pagination="true" rownumbers="true" fitcolumns="true"
    singleselect="true">
    <thead>
        <tr>
            <th field="LoginName" width="50">
                顾客用户名
            </th>
            <th field="LoginPassword" width="50">
                顾客密码
            </th>
        </tr>
    </thead>
    <tr>
        <th field="LoginName" width="50">
        </th>
        <th field="LoginPassword" width="50">
        </th>
    </tr>
</table>
<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true"
        onclick="newUser()">创建</a> <a href="javascript:void(0)" class="easyui-linkbutton"
            iconcls="icon-edit" plain="true" onclick="editUser()">编辑</a> <a href="javascript:void(0)"
                class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destroyUser()">
                删除</a>
</div>
<div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px"
    closed="true" buttons="#dlg-buttons">
    <div class="ftitle">
        顾客信息</div>
    <form id="fm" method="post" novalidate>
    <div class="fitem">
        <label>
            顾客用户名:</label>
        <input name="LoginName" class="easyui-validatebox" required="true">
    </div>
    <div class="fitem">
        <label>
           顾客密码:</label>
        <input name="LoginPassword" class="easyui-validatebox" required="true">
    </div>
    <input type="hidden" id="ID" name="ID" />

    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveUser()">
        保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
            onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>
<script type="text/javascript">
    var url;
    function newUser() {
        $('#dlg').dialog('open').dialog('setTitle', '创建');
        $('#fm').form('clear');
        url = "@Url.Action("saveCustomer", "customer")";
    }
    function editUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('setTitle', '编辑');
            $('#fm').form('load', row);
            url ="@Url.Action("updateCustomer", "customer")";            
        }
    }
    function saveUser() {
        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                console.info(result);
                if (result.Success==false) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.ErrorMessage
                    });
                } else {
                 $.messager.show({
                        title: 'Success',
                        msg: "成功"
                    });
                    $('#dlg').dialog('close');        // close the dialog
                    $('#dg').datagrid('reload');    // reload the user data
                }
            }
        });
    }
    function destroyUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', '确定要删除吗?', function (r) {
                if (r) {
                    $.post('@Url.Action("deleteCustomer", "customer")', { id: row.ID }, function (result) {
                        if (result.Success) {
                            $('#dg').datagrid('reload');    // reload the user data
                            $.messager.show({    // show error message
                                title: 'Success',
                                msg: "删除成功"
                            });
                        } else {
                            $.messager.show({    // show error message
                                title: 'Error',
                                msg: result.ErrorMessage
                            });
                        }
                    }, 'json');
                }
            });
        }
    }
</script>
<style type="text/css">
    #fm
    {
        margin: 0;
        padding: 10px 30px;
    }
    .ftitle
    {
        font-size: 14px;
        font-weight: bold;
        padding: 5px 0;
        margin-bottom: 10px;
        border-bottom: 1px solid #ccc;
    }
    .fitem
    {
        margin-bottom: 5px;
    }
    .fitem label
    {
        display: inline-block;
        width: 80px;
    }
</style>
